package c.com.simple.learning.redis.resp;

import java.io.*;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;

public class TestResp {
    public static void main(String[] args) throws IOException {

        // 创建socket
        // Socket socket = new Socket("127.0.0.1", 6379);
        //
        // // 获取输出流
        // PrintWriter writer = new PrintWriter(new OutputStreamWriter(socket.getOutputStream(), StandardCharsets.UTF_8));
        //
        // InputStream inputStream = socket.getInputStream();
        //
        // BufferedReader reader = new BufferedReader(new InputStreamReader(socket.getInputStream(), StandardCharsets.UTF_8));
        //
        // // 发送数据
        // // sendData(writer, "set", "name", "张三");
        // // 接收数据
        // // Object o = handleResponse(reader);
        // // System.out.println(o);
        //
        // sendData(writer, "get", "name");
        // Object o = handleResponse(reader);
        // System.out.println(o);
        //
        // sendData(writer, "get", "age");
        // o = handleResponse(reader);
        // System.out.println(o);
        //
        // sendData(writer, "get", "sex");
        // o = handleResponse(reader);
        // System.out.println(o);
        //
        // sendData(writer, "mget", "name", "age", "sex");
        // o = handleResponse(reader);
        // System.out.println(o);
        //
        // reader.close();
        // writer.close();
        // socket.close();
        FileInputStream fileInputStream = new FileInputStream("test.txt");
        Object data = getData(fileInputStream);
        System.out.println(data);
    }

    private static Object getData(InputStream inputStream) throws IOException {
        List<Object> list = new ArrayList<>();
        byte[] bytes = new byte[1024];
        int n;
        while ((n = inputStream.read(bytes)) != -1) {
            String s = new String(bytes, 0, n);
            list.add(s);
        }
        return list;
    }

    private static void sendData(PrintWriter writer, String... args) {
        writer.println("*" + args.length);

        for (String arg : args) {
            writer.println("$" + arg.getBytes(StandardCharsets.UTF_8).length);
            writer.println(arg);
        }
        writer.flush();
    }

    private static Object handleResponse(InputStream inputStream) throws IOException {
        int prefix = inputStream.read();
        switch (prefix) {
            case '+':
                return getData(inputStream);
            case '-':
                throw new RuntimeException((String) getData(inputStream));
            case ':':
                return Long.parseLong((String) getData(inputStream));
            case '$':
                // 先读取长度
                int dataLength = Integer.parseInt((String) getData(inputStream));
                // 在读取数据
                if (dataLength == 0) {
                    return null;
                }
                if (dataLength == -1) {
                    return "";
                }
                return getData(inputStream);
            case '*':
                return handleArrayType(inputStream);
            default:
                throw new RuntimeException("不支持的数据格式");
        }
    }

    private static Object handleResponse(BufferedReader reader) throws IOException {
        int prefix = reader.read();
        switch (prefix) {
            case '+':
                return reader.readLine();
            case '-':
                throw new RuntimeException(reader.readLine());
            case ':':
                return Long.parseLong(reader.readLine());
            case '$':
                // 先读取长度
                int dataLength = Integer.parseInt(reader.readLine());
                // 在读取数据
                if (dataLength == 0) {
                    return null;
                }
                if (dataLength == -1) {
                    return "";
                }
                return reader.readLine();
            case '*':
                return handleArrayType(reader);
            default:
                throw new RuntimeException("不支持的数据格式");
        }
    }

    private static Object handleArrayType(BufferedReader reader) throws IOException {

        // 获取数组长度
        int dataLength = Integer.parseInt(reader.readLine());
        if (dataLength <= 0) {
            return null;
        }

        List<Object> list = new ArrayList<>(dataLength);

        for (int i = 0; i < dataLength; i++) {
            list.add(handleResponse(reader));
        }

        return list;
    }

    private static Object handleArrayType(InputStream inputStream) throws IOException {

        // 获取数组长度
        int dataLength = inputStream.read();
        if (dataLength <= 0) {
            return null;
        }

        List<Object> list = new ArrayList<>(dataLength);

        for (int i = 0; i < dataLength; i++) {
            list.add(handleResponse(inputStream));
        }

        return list;
    }


}